<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
class Atom_Entry_Author_Name implements Atom_ChildElement {
    
    const ELEMENT_NAME = "name";
    
    private $name;
    
    public function __construct($name) {
        $this->setName($name);
    }
    
    private function setName($name) {
        if(empty($name)) {
            throw new Exception("Name cannot be set empty.");
        }
        //$this->Name = htmlentities($name, ENT_QUOTES, "UTF-8", false);
        $this->Name = htmlspecialchars($name, ENT_QUOTES, "UTF-8", false);
    }
    
    public function getName() {
        return $this->Name;
    }
    
    /**
     * This should return something similar to:
     * <Name>http://lobstermonster.org/tuomas</Name>
     * 
     * @return DOMElement
     */
    public function asDomElement(DOMDocument $dom) {
        return $dom->createElement(self::ELEMENT_NAME, $this->Name);
    }
    
    /**
     * @param DOMElement $elm
     * @return Atom_Entry_Author_Name
     */
    public static function buildFromDOMElement(DOMElement $elm) {
        return new Atom_Entry_Author_Name($elm->nodeValue);
    }
}